home *** CD-ROM | disk | FTP | other *** search
- /*
- File: ValueStream.cpp
-
- Contains: Stream class implementation for Textension I/O
-
- Written by: Steve Smith
-
- Copyright: © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
- */
-
-
- #ifndef _VALUESTREAM_
- #include "ValueStream.h"
- #endif
-
- // ----- SOM Includes -----
-
- #include <som.xh>
- #include <somobj.xh>
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_ODStorageUnitView_xh
- #include "SUView.xh"
- #endif
-
- #ifndef SOM_Module_OpenDoc_Errors_defined
- #include <ErrorDef.xh>
- #endif
-
- #ifndef __EXCEPT__
- #include <Except.h>
- #endif
-
- // ----- OpenDoc Utilities -----
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef _STORUTIL_
- #include <StorUtil.h>
- #endif
-
- #pragma segment TextEditorUtils
-
- //==============================================================================
- // CLASS SUValueStream
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // SUValueStream::SUValueStream
- //------------------------------------------------------------------------------
-
- SUValueStream::SUValueStream() :
- fView(NULL)
- {
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::~SUValueStream
- //------------------------------------------------------------------------------
-
- SUValueStream::~SUValueStream()
- {
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::InitValueStream
- //------------------------------------------------------------------------------
-
- void SUValueStream::InitValueStream(ODStorageUnitView *view)
- {
- if ( fView )
- // should be som_Free()
- ODDeleteObject(fView);
-
- CStream::IStream();
- fView = view;
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::Free
- //------------------------------------------------------------------------------
-
- void SUValueStream::Free()
- {
- if ( this->ValidateStorageUnitView() )
- ODDeleteObject(fView);
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::WriteBytes
- //------------------------------------------------------------------------------
-
- OSErr SUValueStream::WriteBytes(const void* theBytes, long bytesCount)
- {
- if ( this->ValidateStorageUnitView() )
- {
- Environment* ev = somGetGlobalEnvironment();
-
- TRY
- StorageUnitViewSetValue(fView, ev, bytesCount, (ODValue)theBytes);
- CATCH_ALL
- // We can't throw from a Textension class.
- return ErrorCode();
- ENDTRY
-
- return noErr;
- }
-
- return kODErrInvalidStorageUnit;
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::ReadBytes
- //------------------------------------------------------------------------------
-
- OSErr SUValueStream::ReadBytes(void* theBytes, long bytesCount)
- {
- if ( this->ValidateStorageUnitView() )
- {
- Environment* ev = somGetGlobalEnvironment();
- TRY
- StorageUnitViewGetValue(fView, ev, bytesCount, theBytes);
- CATCH_ALL
- // We can't throw from a Textension class.
- return ErrorCode();
- ENDTRY
-
- return noErr;
- }
-
- return kODErrInvalidStorageUnit;
- }
-
-
- //------------------------------------------------------------------------------
- // SUValueStream::GetPosition
- //------------------------------------------------------------------------------
-
- long SUValueStream::GetPosition() const
- {
- if ( this->ValidateStorageUnitView() )
- {
- TRY
- Environment* ev = somGetGlobalEnvironment();
- return fView->GetOffset(ev);
- CATCH_ALL
- // We can't throw from a Textension class.
- ENDTRY
- }
-
- return 0;
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::SetPosition
- //------------------------------------------------------------------------------
-
- void SUValueStream::SetPosition(long newPosition)
- {
- if ( this->ValidateStorageUnitView() )
- {
- TRY
- Environment* ev = somGetGlobalEnvironment();
-
- char padByte = 0x00;
- long numBytes = 0;
- long curSize = 0;
- curSize = fView->GetSize(ev);
-
- if ( curSize < newPosition )
- numBytes = newPosition - curSize;
-
- fView->SetOffset(ev, curSize);
-
- for (int i = 0; i < numBytes; i++)
- {
- StorageUnitViewSetValue(fView, ev, 1, &padByte);
- }
-
- fView->SetOffset(ev, newPosition);
- CATCH_ALL
- // We can't throw from a Textension class.
- ENDTRY
- }
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::Skip
- //------------------------------------------------------------------------------
-
- void SUValueStream::Skip(long count)
- {
- if ( this->ValidateStorageUnitView() )
- {
- TRY
- Environment* ev = somGetGlobalEnvironment();
- this->SetPosition(fView->GetOffset(ev) + count);
- CATCH_ALL
- // We can't throw from a Textension class.
- ENDTRY
- }
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::GetSize
- //------------------------------------------------------------------------------
-
- long SUValueStream::GetSize() const
- {
- if ( this->ValidateStorageUnitView() )
- {
- TRY
- Environment* ev = somGetGlobalEnvironment();
- return fView->GetSize(ev);
- CATCH_ALL
- // We can't throw from a Textension class.
- ENDTRY
- }
-
- return 0;
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::Append
- //------------------------------------------------------------------------------
-
- OSErr SUValueStream::Append(long count)
- {
- if ( this->ValidateStorageUnitView() )
- {
- TRY
- Environment* ev = somGetGlobalEnvironment();
-
- char padByte = 0x00;
- long curSize = fView->GetSize(ev);
- long curPosition = fView->GetOffset(ev);
-
- fView->SetOffset(ev, curSize);
-
- for (int i = 0; i < count; i++)
- {
- StorageUnitViewSetValue(fView, ev, 1, &padByte);
- }
-
- fView->SetOffset(ev, curPosition);
- CATCH_ALL
- // We can't throw from a Textension class.
- return ErrorCode();
- ENDTRY
-
- return noErr;
- }
-
- return kODErrInvalidStorageUnit;
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::Empty
- //------------------------------------------------------------------------------
-
- void SUValueStream::Empty()
- {
- if ( this->ValidateStorageUnitView() )
- {
- TRY
- Environment* ev = somGetGlobalEnvironment();
- fView->SetOffset(ev, 0);
- fView->DeleteValue(ev, fView->GetSize(ev));
- CATCH_ALL
- // We can't throw from a Textension class.
- ENDTRY
- }
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::Load
- //------------------------------------------------------------------------------
-
- OSErr SUValueStream::Load(long size, Ptr* data)
- {
- if ( this->ValidateStorageUnitView() )
- return noErr;
-
- return kODErrInvalidStorageUnit;
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::Unload
- //------------------------------------------------------------------------------
-
- void SUValueStream::Unload(Ptr data)
- {
- if ( this->ValidateStorageUnitView() )
- {
- }
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::GetStorageUnit
- //------------------------------------------------------------------------------
-
- ODStorageUnit *SUValueStream::GetStorageUnit()
- {
- if ( this->ValidateStorageUnitView() )
- {
- TRY
- Environment* ev = somGetGlobalEnvironment();
- return fView->GetStorageUnit(ev);
- CATCH_ALL
- // We can't throw from a Textension class.
- ENDTRY
- }
-
- return nil;
- }
-
- //------------------------------------------------------------------------------
- // SUValueStream::ValidateStorageUnitView
- //------------------------------------------------------------------------------
-
- Boolean SUValueStream::ValidateStorageUnitView() const
- {
- if ( !fView )
- return false;
-
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-